<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'updateCategory.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<link type="text/css" rel="stylesheet" href="/easybuy/css/ProductCategoryList.css" />
	<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
	<script type="text/javascript">
	$(function(){
		istu();
		parentName($("#type").val());
		$("#file2").change(function(){
			getPath(document.getElementById("file2"));
		});
		$("#span2").click(function(){
			location.href="ProductCategoryList.jsp";
		});
		$("#type").change(function(){
			parentName($("#type").val());
			$("#parentType option").each(function(){
				if($(this).text()==$("#name").val()){
					$(this).remove();
				}
			});
			istu();
		});
		function deleParentName(index){
			document.getElementById("parentName").options.remove(index);
		}
		$("#form").submit(function(){
			if($("#name").val()==""){
				alert("名称不能为空！");
				return false;
			} else if ($("#type").val()==1 && ($("#file2").val()=="" || $("#file2").val()==undefined) && ($("#fi").val()=="" || $("#fi").val()==undefined)){
				alert("请选择文件！");
				return false;
			} else {
				return true;
				
			}
		});
	});
	//读取图片本地完整路径
	function getPath(fileDom) {  
		//判断是否支持FileReader  
		if (window.FileReader) {  
   			var reader = new FileReader();  
		} else {  
   			alert("您的设备不支持图片预览功能，如需该功能请升级您的设备！");  
		}  
		//获取文件  
	    var file = fileDom.files[0];  
	    var imageType = /^image\//;  
	    //是否是图片  
	    if (!imageType.test(file.type)) {  
	        alert("仅支持图片！");  
	        return;  
	    }  
	    //读取完成  
	    reader.onload = function (e) {  
	        //获取图片dom  
	        var img = document.getElementById("img2");  
	        //图片路径设置为读取的图片  
	        img.src = e.target.result;  
	    };  
	    reader.readAsDataURL(file);  
	}  
	function parentName(type){
		$.ajax({
			"url" : "category",
			"type" : "post",
			async:false,
			"data" : "opr=parentName&type="+type,
			"dataType" : "json",
			"success" : function(data) {
				$("#parentType").empty();
				if(type==1){
					$("#parentType").append("<option value='0'>无</option>");
				}else{
					for(var i=0;i<data.length;i++){
						$("#parentType").append("<option id='option' value='"+data[i].id+"'>"+data[i].parentName+"</option>");
					}
				}
			}
		});
	};
	function hui(){
		location.href="ProductCategoryList.jsp";
	}
	function istu(){
		if($("#type").val()==1){
			$("#tu").css("display","block");
			$("#file").css("display","block");
		} else {
			$("#tu").css("display","none");
			$("#file").css("display","none");
		}
	}
	</script>	
	
 </head>
  
  <body>
  <input type="hidden" id="yname" value="${category.name }"/>
  <%@ include file="topBar.jsp" %>
  <%@ include file="leftBar.jsp" %>
  <%request.setCharacterEncoding("UTF-8"); %>
   <div id="updateCategory" >
  <form action="category?opr=updateCategoryList" id="form" enctype="multipart/form-data" method="post">
  		<table>
  			<tr>
  				<td colspan="1" align="center">分类信息
  				<input type="text" id="id" name="id" value="${category.id }"/>
  				<input type="hidden" id="pageIndex" name="pageIndex" value="${pageIndex }"/>
  				</td>
  			</tr>
  			<tr>
  				<td>分类名称：
  				<input type="text" name="name" id="name" value="${category.name }"/></td>
  			</tr>
  			<tr>
  				<td>分类级别：
  				<select name="type" id="type">
    				<option value="1" <c:if test="${category.type==1 }">selected="selected"</c:if>>一级菜单</option>
    				<option value="2" <c:if test="${category.type==2 }">selected="selected"</c:if>>二级菜单</option>
    				<option value="3" <c:if test="${category.type==3 }">selected="selected"</c:if>>三级菜单</option>
    			</select>
  				</td>
  			</tr>
  			<tr>
  				<td>父级分类：
  					<select id="parentType" name="parentType">
  						
  					</select>
				</td>
  			</tr>
  			<tr id="tu">
	    		<td style='text-indent: 1em'><input type="text" id="fi" name="fi" value="${category.icon }"/>图标：
	    		<p id="pimg2"><img src="images/${category.icon }" id="img2" name="icon"/></p>
				<input type="file" id="file2" name="nfile" accept="image/*"/>
				</td>
		    </tr>
  			<tr id="bbtn">
  				<td><input type="submit" value="保存" id="save"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="重置"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  				<input type="button" value="返回" onclick="hui();"/><span id="span2">×</span></td>
  			</tr>
  		</table>
  	</form>
  	</div>
  	<%@ include file="bottomBar.jsp" %>
  </body>
</html>
